Msg("====Time Spent module initialized...\n")

function gSAM.StartTimeSpent(ply)
	if gSAM.PlayerVarExists(ply:SteamID(),"JTime") then
		gSAM.SetPlayerVar(ply:SteamID(),"JTime",CurTime())
		gSAM.SetPlayerVar(ply:SteamID(),"TimeSpent",0)
	else
		gSAM.CreatePlayerVar(ply:SteamID(),"JTime",CurTime())
		gSAM.CreatePlayerVar(ply:SteamID(),"TimeSpent",0)
	end
end
hook.Add("PlayerInitialSpawn","gSAM.StartTimeSpent",gSAM.StartTimeSpent)

function gSAM.GetPlayerCurTime(ply)
	local time = CurTime() - gSAM.GetPlayerVar(ply:SteamID(),"JTime")
	return time
end

function gSAM.GetPlayerTime(ply)
	local time = gSAM.GetPlayerCurTime(ply) + gSAM.GetPlayerVar(ply:SteamID(),"TimeSpent")
	return time
end

function gSAM.GetFormattedPlayerTime(ply)
	local sec = gSAM.GetPlayerTime(ply)
	local min = math.floor(sec / 60)
	local hour = math.floor(min / 60)
	local day = math.floor(hour / 24)
	local week = math.floor(day / 7)
	return week.." - "..day.." - "..hour..":"..min..":"..sec
end

function gSAM.GetFormattedPlayerCurTime(ply)
	local sec = gSAM.GetPlayerCurTime(ply)
	local min = math.floor(sec / 60)
	local hour = math.floor(min / 60)
	local day = math.floor(hour / 24)
	local week = math.floor(day / 7)
	return week.." - "..day.." - "..hour..":"..min..":"..sec
end

function gSAM.EndTimeSpent(ply)
	local time = gSAM.GetPlayerTime(ply)
	gSAM.SetPlayerVar(ply:SteamID(),"TimeSpent",time)
end
hook.Add("PlayerDisconected","gSAM.EndTimeSpent",gSAM.EndTimeSpent)
